package com.imc.platform.demo.controller;

import com.imc.core.annotation.Logs;
import com.imc.core.utils.ExceptionUtil;
import com.imc.platform.base.user.pojo.User;
import com.imc.platform.demo.service.UserService;
import com.twp.common.component.process.exception.combo.IComboExecptionForTFunc;
import com.twp.common.tuple.ResultDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by tanweiping on 2018/7/19.
 */
@Slf4j
@RestController
public class TestCacheCOntroller {

    @Autowired
    IComboExecptionForTFunc iComboExecptionForTFunc;

    @Autowired
    private CacheManager cacheManager;

    @Autowired
    UserService userService;

    @GetMapping("/test/{name}")
    public String test(@PathVariable String name){
        userService.findAll(name);
        return "eqwewq";
    }


    @Logs
    @GetMapping("/test/data/{name}")
    public ResultDTO testData(@PathVariable String name) throws Exception {
        if (true){
            throw new Exception("2");
        }
        return iComboExecptionForTFunc.executeFlow(
                //定义返回
                ResultDTO.builder().build(),
                (it)-> {
                    //业务处理
                    it.setMessage("XXXX操作成功");
                    //it.setData(new com.imc.core.pojo.User()));
                },

                (e, it) -> {
                    //异常处理
                    //获取异常链
                    Throwable throwable = ExceptionUtil.getRootCause(e);
                    log.error("throwable:"+throwable.getMessage());
                    it.setMessage(throwable.getMessage());
                    it.setIsSuccess(false);
                },it->{
                    //finally
                    //资源关闭
                }
        );
    }

    @GetMapping("/test/getUserData")
    public ResultDTO getUserData(@PageableDefault(page = 0,size = 2) Pageable pageable, User user){
        return iComboExecptionForTFunc.executeFlow(
                //定义返回
                ResultDTO.builder().build(),
                (it)-> {
                    //业务处理
                    it.setMessage("XXXX操作成功");
                    it.setData(userService.lamda(pageable,user));
                    //it.setData(new com.imc.core.pojo.User()));
                },
                (e, it) -> {
                    //异常处理
                    //获取异常链
                    Throwable throwable = ExceptionUtil.getRootCause(e);
                    log.error("throwable:"+throwable.getMessage());
                    it.setMessage(throwable.getMessage());
                    it.setIsSuccess(false);
                },it->{
                    //finally
                    //资源关闭
                }
        );
    }
}
